#include "44b0x.h"
#include "uTypes.h"
#include "sysUtils.h"
#include "D12Def.h" 
#include "UsbDesc.h"
#include "console.h"

static U8 usb_remote_wake;
static U8 usb_config;
static U8 UsbSendDescF;

static int send_remain;
static U32 send_ptr;

void sysUtilsDelay(U32 Time);

#define	LEDPort	rPDATC
#define	LED0_ON	0x1f8
#define	LED1_ON	0x1f4
#define	LED2_ON	0x1f2
#define	ShowLed(LedStatus)	LEDPort = (LedStatus)

U8 UsbInit()
{
	UsbSendDescF = 0;

	if(D12RdChipId()!=0x1210 )
		return STATUS_ERR;

	D12SetDma(MyD12DmaCfg);
	if(D12GetDma()!=MyD12DmaCfg)
		return STATUS_ERR;

	D12SetMode(MyD12EpCfgOff, D12Pll24M);
	sysUtilsDelay(20000);
	D12SetMode(MyD12EpCfgOn, D12Pll24M);

	return STATUS_OK;
}

///////////////////////////////

#define	UsbDevDescType	1
#define	UsbCfgDescType	2
#define	UsbStrDescType	3
#define	UsbItfDescType	4
#define	UsbEndpDescType	5
#define	UsbPwrDescType	6

#define	UsbDevDescSize	18
#define	UsbCfgDescSize	9
#define	UsbItfDescSize	9
#define	UsbEndpDescSize	7

#define	UsbEndpControl		0
#define	UsbEndpIsochronous	1
#define	UsbEndpBulk			2
#define	UsbEndpInterrupt	3	

////////////////////////////////////////

#define	UsbClassMassStorage	8

#define	UsbSubClassRbc		1
#define	UsbSubClassSff8020i	2
#define	UsbSubClassQic157	3
#define	UsbSubClassUfi		4
#define	UsbSubClassSff8070i	5
#define	UsbSubClassScsi		6

#define	UsbProtocolCbi0	0
#define	UsbProtocolCbi1	1
#define	UsbProtocolBulk	0x50
////////////////////////////////////////

#define	Ep0PacketSize	16
#define	Ep2PacketSize	64
#define	EndpCnt	2
#define	UsbTotalCfgDescSize	UsbCfgDescSize+UsbItfDescSize+EndpCnt*UsbEndpDescSize
static __packed struct{
	UsbDevDesc DevDesc;
	U8 CfgDesc[9];
	U8 ItfDesc[9];
	U8 Endp4Desc[7];
	U8 Endp5Desc[7];

}ThisDevDesc =
				{
					{
						UsbDevDescSize,
						UsbDevDescType,
						0x110,
						0, 0, 0,
						Ep0PacketSize,
						0x471,
						0x222,
						0x113,
						0, 0, 0,
						1
					},
					{
						UsbCfgDescSize,
						UsbCfgDescType,
						UsbTotalCfgDescSize, 0,
						1,
						1,
						0,
						0x80,
						0x32
					},
					{
						UsbItfDescSize,
						UsbItfDescType,
						0,
						0,
						EndpCnt,
						UsbClassMassStorage,
						UsbSubClassUfi,
						UsbProtocolBulk,
						0
					},
					{
						UsbEndpDescSize,
						UsbEndpDescType,
						0x82,
						UsbEndpBulk,
						Ep2PacketSize,
						0,
						0
					},
					{
						UsbEndpDescSize,
						UsbEndpDescType,
						0x2,
						UsbEndpBulk,
						Ep2PacketSize,
						0,
						0
					}					
				};

/****************** standard device request ****************/
U8 UsbGetStatus(U8 *SetupPkt)
{
#ifdef DEBUG
  printf ("UsbGetStatus ().\n");
#endif

	if(SetupPkt[2]|SetupPkt[3]|SetupPkt[5]|SetupPkt[7])
		return 1;
	if(SetupPkt[6]!=2)
		return 1;
	if(SetupPkt[0]==0x80)
	{
		if(SetupPkt[4])
			return 1;
		SetupPkt[0] = usb_remote_wake?1:0;
		SetupPkt[1] = 0;
	}
	else
	if(SetupPkt[0]==0x81)
	{
		if(SetupPkt[4])
			return 1;
		SetupPkt[0] = 0;
		SetupPkt[1] = 0;		
	}
	else
	if(SetupPkt[0]==0x82)
	{
		D12SelEp((SetupPkt[4]*2)|((SetupPkt[4]&0x80)?1:0));
		SetupPkt[1] = (D12Dat&2)?1:0;
		SetupPkt[0] = 0;
	}
	else
		return 1;

	D12WrEp(1, SetupPkt, 2);
	return 0;		
}

U8 UsbClrFeature(U8 *SetupPkt)
{
#ifdef DEBUG
  printf ("UsbClrFeature ().\n");
#endif 

	if(SetupPkt[0]&0x80)
		return STATUS_ERR;
	if(SetupPkt[3]|SetupPkt[5]|SetupPkt[6]|SetupPkt[7])	
		return STATUS_ERR;
		
	if(SetupPkt[0]&0x1f)
	{
		if((SetupPkt[0]&0x1f)!=2)
			return STATUS_ERR;
		D12SetEpStat((SetupPkt[4]*2)|((SetupPkt[4]&0x80)?1:0),0);
	}
	else
	{
		if(SetupPkt[4])
			return STATUS_ERR;
		usb_remote_wake = 0;
	}
	
	D12WrEp(1, SetupPkt, 0);
	return STATUS_OK;
}

U8 UsbSetFeature(U8 *SetupPkt)
{

#ifdef DEBUG
  printf ("UsbSetFeature ().\n");
#endif 

	if(!(SetupPkt[0]&0x80))
		return STATUS_ERR;
	if(SetupPkt[3]|SetupPkt[5]|SetupPkt[6]|SetupPkt[7])
		return STATUS_ERR;
		
	if(SetupPkt[0]&0x1f)
	{
		if((SetupPkt[0]&0x1f)!=2)
			return STATUS_ERR;
		D12SetEpStat((SetupPkt[4]*2)|((SetupPkt[4]&0x80)?1:0),1);
	}
	else
	{
		if(SetupPkt[4])
			return STATUS_ERR;
		usb_remote_wake = 1;
	}
	
	D12WrEp(1, SetupPkt, 0);
	return STATUS_OK;
}

U8 UsbSetAddress(U8 *SetupPkt)
{

#ifdef DEBUG
  printf ("UsbSetAddress ().\n");
#endif 

	if(SetupPkt[0]|SetupPkt[3]|SetupPkt[4]|SetupPkt[5]|SetupPkt[6]|SetupPkt[7])
		return STATUS_ERR;
		
	D12SetAddr(SetupPkt[2]|0x80);
	D12WrEp(1, SetupPkt, 0);
	return STATUS_OK;	
}

void UsbSendDesc()
{
//	U8 i;

	UsbSendDescF = send_remain>=Ep0PacketSize;	
	D12WrEp(1, (U8 *)send_ptr, (send_remain>Ep0PacketSize)?Ep0PacketSize:send_remain);
	
//	putch('@');
//	for(i=0; i<((send_remain>Ep0PacketSize)?Ep0PacketSize:send_remain); i++)
//		printf("%x,", *(U8 *)(send_ptr+i));	
		
	send_remain -= Ep0PacketSize;
	send_ptr    += Ep0PacketSize;
	
}

U8 UsbGetDescriptor(U8 *SetupPkt)
{
#ifdef DEBUG
  printf ("UsbGetDescriptor (). \n");
#endif
	if(SetupPkt[0]!=0x80)
		return STATUS_ERR;
		
	switch	(SetupPkt[3])
	{
	case UsbDevDescType:
#ifdef DEBUG
       printf ("UsbGetDescriptor (). Get Device Descriptor.\n");
#endif
		send_ptr = (U32)&ThisDevDesc;
		send_remain = UsbDevDescSize;
		break;
	case UsbCfgDescType:
#ifdef DEBUG
       printf ("UsbGetDescriptor (). Get Config descriptor.\n");
#endif
		send_ptr = (U32)&ThisDevDesc.CfgDesc;
		send_remain = UsbTotalCfgDescSize;
		break;
	case UsbStrDescType:		
		return STATUS_ERR;	
	default:
		return STATUS_ERR;	
	}

	if(!SetupPkt[7]&&(SetupPkt[6]<send_remain))
		send_remain = SetupPkt[6];

	UsbSendDesc();	
	return STATUS_OK;	
}

U8 UsbGetConfiguration(U8 *SetupPkt)
{
#ifndef DEBUG
  printf ("UsbGetConfiguration ().\n");
#endif
	if(SetupPkt[0]!=0x80)
		return STATUS_ERR;
	if(SetupPkt[2]|SetupPkt[3]|SetupPkt[4]|SetupPkt[5]|SetupPkt[7])
		return STATUS_ERR;
	if(SetupPkt[6]!=1)
		return STATUS_ERR;
		
	SetupPkt[0] = usb_config?1:0;
	D12WrEp(1, SetupPkt, 1);	
	return STATUS_OK;	
}

U8 UsbSetConfiguration(U8 *SetupPkt)
{
#ifdef DEBUG
  printf ("UsbSetConfiguration ().\n");
#endif

	if(SetupPkt[0]|SetupPkt[3]|SetupPkt[4]|SetupPkt[5]|SetupPkt[6]|SetupPkt[7])
		return STATUS_ERR;
	if(SetupPkt[2]&0xfe)
		return STATUS_ERR;
		
	usb_config = SetupPkt[2]&1;
	D12SetEp(usb_config);
	D12SetEpStat(4, usb_config^1);
	D12SetEpStat(5, usb_config^1);
	D12WrEp(1, SetupPkt, 0);
	return STATUS_OK;	
}

U8 UsbGetInterface(U8 *SetupPkt)
{
#ifdef DEBUG
  printf ("UsbGetInterface ().\n");
#endif
	if(SetupPkt[0]!=0x81)
		return STATUS_ERR;
	if(SetupPkt[2]|SetupPkt[3]|SetupPkt[4]|SetupPkt[5]|SetupPkt[7])
		return STATUS_ERR;
	if(SetupPkt[6]!=1)
		return STATUS_ERR;
		
	SetupPkt[0] = 0;
	D12WrEp(1, SetupPkt, 1);
	return STATUS_OK;
}

U8 UsbSetInterface(U8 *SetupPkt)
{
	return STATUS_ERR;
}

U8 UsbReserved(U8 *SetupPkt)
{
	return STATUS_ERR;	
}

U8 (*StdDevReq[])(U8 *SetupPkt) = {
	UsbGetStatus,
	UsbClrFeature,
	UsbReserved,
	UsbSetFeature,
	UsbReserved,
	UsbSetAddress,
	UsbGetDescriptor,
	UsbReserved,
	UsbGetConfiguration,
	UsbSetConfiguration,
	UsbGetInterface,
	UsbSetInterface,
	UsbReserved,
	UsbReserved,
	UsbReserved,
	UsbReserved
}; 

/********************** D12 interrupt process ******************/
void D12Ep0IntProc()
{
	U8 SetupPkt[8];
	U8 i;	
	
	UsbSendDescF = 0;

#ifdef DEBUG
  printf ("D12Ep0IntProc ().\n");
#endif
	
	D12RdLastTrStat(0);
	if(D12Cmd&0x20)	
		if(D12RdEp(0, SetupPkt, 8)==8)
		{
			D12AckEp(0);
			D12AckEp(1);

#ifdef DEBUG
			putch('\n');
          for(i=0; i<8; i++)
        		printf("%x,", SetupPkt[i]);
#endif 				
			if(!StdDevReq[SetupPkt[1]&0xf](SetupPkt));
				return;
		}
					
	D12SetEpStat(0, D12EpStall);
	D12SetEpStat(1, D12EpStall);		
}

void D12Ep1IntProc()
{
	U8 i;

	i = D12RdLastTrStat(1);
	if(UsbSendDescF)
		UsbSendDesc();
		
}

void D12Ep2IntProc()
{
	D12SetEpStat(2, D12EpStall);
}

void D12Ep3IntProc()
{
	D12SetEpStat(3, D12EpStall);
}

void D12Ep4IntProc()
{
  int i;
	U8 size;
	U8 Ep2OutBuf[Ep2PacketSize];

#ifdef DEBUG
  printf ("D12Ep4IntProc ().\n");
#endif

	D12RdLastTrStat(4);	
	size = D12RdEp(4, Ep2OutBuf, Ep2PacketSize);
	
	if((size==6)&&(*(U32 *)Ep2OutBuf==0x12345678))	
	{
	  ShowLed(Ep2OutBuf[4]+(Ep2OutBuf[5]<<8));
    printf ("Data Received:");	  
	  for (i = 0; i < 6; ++ i)
	  {
      printf ("0x%02x ", Ep2OutBuf[i]);
	  }
	  printf ("\n");
	}
}

void D12Ep5IntProc()
{
}

void D12BusRstProc()
{
}

void D12SuspChgProc()
{
}